<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>用户注册界面</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <style>
        html, body {
            height: 100%;
            margin: 0;
            font-family: 'Roboto', sans-serif;
            background: linear-gradient(135deg, #f0f8ff, #e6f7ff);
            display: flex;
            justify-content: center;
            align-items: center;
        }

        body {
            background: linear-gradient(135deg, #f0f8ff, #e6f7ff);
        }

        h1 {
            font-size: 28px;
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        .container {
            background: #ffffff;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
            width: 350px;
            text-align: center;
        }

        form {
            width: 100%;
        }

        label {
            font-size: 18px;
            font-weight: bold;
            display: block;
            margin-bottom: 10px;
            text-align: left;
        }

        input {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        .required {
            color: red;
            font-size: 14px;
            display: inline-block;
            margin-right: 5px;
        }

        .submit {
            background: #007bff;
            border: none;
            color: #ffffff;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
            width: 100%;
        }

        .submit:hover {
            background-color: #0056b3;
        }

        a {
            text-decoration: none;
            color: #007bff;
            font-weight: bold;
            display: block;
            margin-top: 10px;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
<div class="container">
    <h1>新用户注册</h1>
    <form id="registerForm" action="/user/register" method="post">
        <label for="username">
            <span class="required">*</span>用户名
        </label>
        <input type="text" name="username" id="username" placeholder="请输入用户名" class="register" required>

        <label for="pwd">
            <span class="required">*</span>登录密码
        </label>
        <input type="password" name="password" id="pwd" placeholder="请输入密码" class="register" required>

        <label for="c_pwd">
            <span class="required">*</span>确认密码
        </label>
        <input type="password" name="c_pwd" id="c_pwd" placeholder="请确认密码" class="register" required>

        <button type="submit" class="submit">注册</button>

        <a href="/login">去登录</a>
    </form>
</div>
<script>
    document.getElementById('registerForm').addEventListener('submit', function (event) {
        event.preventDefault(); // 阻止表单的默认提交行为

        if (document.getElementById("username").value == "") {
            alert("用户名不能为空");
            return false;
        }
        if (document.getElementById("pwd").value == "") {
            alert("密码不能为空");
            return false;
        }
        if (document.getElementById("c_pwd").value == "") {
            alert("确认密码不能为空");
            return false;
        }
        if (document.getElementById("pwd").value != document.getElementById("c_pwd").value) {
            alert("两次密码不一致");
            return false;
        }

        const formData = new FormData(this);

        fetch('/user/register', {
            method: 'POST',
            body: formData
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok ' + response.statusText);
                }
                return response.json(); // 假设后端返回的是JSON格式的数据
            })
            .then(data => {
                console.log(data);
                if (data.code == 0) {
                    alert("注册成功！");
                    window.location.href = "/login";
                } else {
                    alert("注册失败,用户已经存在！");
                }
            })
            .catch(error => {
                console.error('There was a problem with the fetch operation:', error);
                // 在这里处理错误，比如显示错误信息等
            });
    });
</script>
</body>

</html>
